# coding=utf-8
import bs4
import requests
from PyQt4 import QtGui
from PyQt4 import QtCore

header = {
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Connection': 'keep-alive',
    'Cookie': 'xxxxx',
    'Host': 'www.cnblogs.com',
    'Referer': 'http://passport.cnblogs.com/login.aspx?ReturnUrl=http://www.cnblogs.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.6 Safari/537.36'
}


class Login(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)

        self.setWindowTitle(u'登录')
        self.resize(200, 150)

        # 定义输入框的字段
        self.txtUserName = QtGui.QLineEdit(self)
        self.txtUserName.setPlaceholderText(u'用户名')
        self.txtPassword = QtGui.QLineEdit(self)
        self.txtPassword.setEchoMode(QtGui.QLineEdit.Password)
        self.txtPassword.setPlaceholderText(u'密码')
        self.doLogin = QtGui.QPushButton(u'登录', self)
        self.doCancel = QtGui.QPushButton(u'取消', self)
        self.doLogin.clicked.connect(self.login)
        self.doCancel.clicked.connect(self.reject)

        # 定义布局
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.txtUserName)
        layout.addWidget(self.txtPassword)
        spacerItem = QtGui.QSpacerItem(20, 48, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        layout.addItem(spacerItem)

        # 按钮布局
        buttonLayout = QtGui.QHBoxLayout()
        spancerItem2 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        buttonLayout.addItem(spancerItem2)
        buttonLayout.addWidget(self.doLogin)
        buttonLayout.addWidget(self.doCancel)
        layout.addLayout(buttonLayout)
        self.setLayout(layout)

        self.cookies = None

    def login(self):
        if (self.txtUserName.text() and self.txtPassword.text()):
            url='http://passport.cnblogs.com/login.aspx?ReturnUrl=http://www.cnblogs.com/'
            data = {
                'tbUserName': self.txtUserName.text(),
                'tbPassword': self.txtPassword.text(),
                'chkRemember': 'on',
                'btnLogin': u'登  录',
                'txtReturnUrl': 'http://www.cnblogs.com/',
            }
            r = requests.post(url, data=data)
            self.cookies = r.cookies
            print self.txtUserName.text(), self.txtPassword.text()
            print r.cookies
            print r.headers
            self.accept()

        else:
            QtGui.QMessageBox.warning(self, u'提示', u'用户名或密码错误')

    def doParser(self):
        url = 'http://www.cnblogs.com/MrWho/'
        r = requests.get(url, cookies=self.cookies)
        soup = bs4(r.content)
        print soup



class Upload(QtGui.QMainWindow):
    def __init__(self):
        super(Upload, self).__init__()
        self.initUI()

    def initUI(self):
        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.statusBar()
        self.setFocus()

        login = QtGui.QAction(QtGui.QIcon('login.png'), 'Login', self)
        login.setShortcut('Ctrl+O')
        login.setStatusTip(u'选择文件')
        self.connect(login, QtCore.SIGNAL('triggered()'), self.showDialog)


        openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip(u'选择文件')
        self.connect(openFile, QtCore.SIGNAL('triggered()'), self.showDialog)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(openFile)

        loginMenu = menubar.addMenu('&Login')
        loginMenu.addAction(login)


        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle(u'文件上传')
        self.statusBar().showMessage(u'文件上传')

    def showDialog(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
        fname = open(filename)
        data = fname.read()
        self.textEdit.setText(data)


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    if Login().exec_() == QtGui.QDialog.Accepted:
        ex = Upload()
        ex.show()
        app.exec_()
